package com.juzhencms.apps.busi.web;

import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.annotation.IgnoreLog;
import com.juzhencms.apps.base.common.util.JWTUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

@RestController
@Api(tags = "05.图片验证码")
@Slf4j
@RequestMapping("/validateImg")
public class ValidateImgControllor {
	
	@Autowired
    private JWTUtil jwtUtil;
	
	@ApiOperation(value="获取图片验证码")
	@IgnoreLog //不记录访问日志
	@RequestMapping(value="/img",method = RequestMethod.POST,produces="application/json; charset=UTF-8")
	@ResponseBody	
    public Map img(){
		return jwtUtil.getImgCode();
    }
	
	@ApiOperation(value="验证图片验证码")
	@IgnoreLog //不记录访问日志
	@RequestMapping(value="/validate",method = RequestMethod.POST,produces="application/json; charset=UTF-8")
	@ResponseBody	
    public Map validate(@RequestBody @ApiParam(name="requestBody参数",value="传入json格式，例如:{\"validateCode\":\"1234\",\"token\":\"drnhsrjsrjs.eryhe4dbranadhhjdsanz\"}",required=true) String jsonString){
		Map param=null;
		try {
			param=JSON.parseObject(jsonString, Map.class);
		}catch(Exception e) {
			log.error("参数解析报错", e);
		}
		
		if(param==null || param.get("validateCode")==null || param.get("token").toString().equals("")) {
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "参数有误");
			return result;
		}
		
		String validateCode=param.get("validateCode").toString();
		String token=param.get("token").toString();
		
		if(jwtUtil.validateImgCode(token, validateCode)) {
			Map result=new HashMap();
			result.put("result", 1);
			result.put("msg", "验证成功");
			return result;
		}else{
			Map result=new HashMap();
			result.put("result", 0);
			result.put("msg", "验证码错误");
			return result;
		}
			
		
    }
}
